package com.thinkgem.jeesite.common.utils;

import java.io.PrintStream;
import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Random;
import org.apache.commons.lang3.RandomStringUtils;

public class UtilDate {
	public static final String dtLong = "yyyyMMddHHmmss";
	public static final String dtLongsss = "yyyyMMddHHmmssSSS";
	public static final String simple = "yyyy-MM-dd HH:mm:ss";
	public static final String simpleMinute = "HH:mm";
	public static final String dateF = "yyyy-MM-dd";
	public static final String dtShort = "yyyyMMdd";

	public static String getOrderNum() {
		Date date = new Date();
		DateFormat df = new SimpleDateFormat("yyyyMMddHHmmssSSS");
		return df.format(date);
	}

	public static String getDateFormatter() {
		Date date = new Date();
		DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		return df.format(date);
	}

	public static String getMinuteFormatter() {
		Date date = new Date();
		DateFormat df = new SimpleDateFormat("HH:mm");
		return df.format(date);
	}

	public static String getDate() {
		Date date = new Date();
		DateFormat df = new SimpleDateFormat("yyyyMMdd");
		return df.format(date);
	}

	public static Date formatDate(String dateStr) throws ParseException {
		DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
		if (dateStr.length() > 10) {
			return df.parse(dateStr.substring(0, 10));
		}
		return df.parse(dateStr);
	}

	public static String getThree() {
		Random rad = new Random();
		return rad.nextInt(1000) + "";
	}

	public static String getThreeRandom() {
		return RandomStringUtils.randomNumeric(3);
	}

	public static void main(String[] agrs) throws ParseException {
		long days = getIntervalDays("2011-11-22", "2011-12-25");
		System.out.println(days);
	}

	public static Timestamp getCurrentTimestamp() {
		Timestamp now = new Timestamp(System.currentTimeMillis());
		return now;
	}

	public static String getStrTime(Timestamp time) {
		DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		return df.format(time);
	}

	public static String getStrDate(Timestamp time) {
		DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
		return df.format(time);
	}

	public static String getStrHourMinute(Timestamp time) {
		DateFormat df = new SimpleDateFormat("HH:mm");
		return df.format(time);
	}

	public static String getStrTime(Date date) {
		DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		return df.format(date);
	}

	public static String getStrDate(Date date) {
		DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
		return df.format(date);
	}

	public static String getStrHourMinute(Date date) {
		DateFormat df = new SimpleDateFormat("HH:mm");
		return df.format(date);
	}

	public static Timestamp addDays(int days, Timestamp ts) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(new Date(ts.getTime()));
		cal.add(5, days);
		return new Timestamp(cal.getTime().getTime());
	}

	public static Timestamp addHours(int hours, Timestamp ts) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(new Date(ts.getTime()));
		cal.add(10, hours);
		return new Timestamp(cal.getTime().getTime());
	}

	public static long getIntervalDays(String beginDate, String endDate) throws ParseException {
		long beginD = formatDate(beginDate).getTime();
		long endD = formatDate(endDate).getTime();
		long between_days = (endD - beginD) / 86400000L;
		return between_days;
	}

	public static long getIntervalDays(Date beginDate, Date endDate) throws ParseException {
		long beginD = beginDate.getTime();
		long endD = endDate.getTime();
		long between_days = (endD - beginD) / 86400000L;
		return between_days;
	}

	public static long getIntervalHours(Date beginDate, Date endDate) throws ParseException {
		long beginD = beginDate.getTime();
		long endD = endDate.getTime();
		long between_days = (endD - beginD) / 3600000L;
		return between_days;
	}

	public static long getIntervalSeconds(Date beginDate, Date endDate) throws ParseException {
		long beginD = beginDate.getTime();
		long endD = endDate.getTime();
		long between_days = (endD - beginD) / 1000L;
		return between_days;
	}
}
